var actclubshop_pro = require('../../../model/clubshop_pro')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    yihuoconfig:{
      type: Object,
      value:{}
    },
    themcolor: {
      type: Object,
      observer: function (newval, oldval) {

      },
    },
    level: {
      type: Number,
      value: 1
    },
    bclass: {
      type: Array,
      value: []
    },
    sclass: {
      type: Object,
      value: {}
    }
  },
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },

  /**
   * 组件的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    classshow: false,
    isshow: false,
    model: null,
    actmodel: "model",
    // themcolor: {},
    autosize: {
      maxHeight: 10,
      minHeight: 1
    },
    vildfield: {
      name: true,
      bimg: true,
      simg: true,
      costprice: true,
      price: true,
      realprice: true,
      stock: true,
      bpid:true,
      spid:true,
    },
    vildfieldmesage: {
      name: "请填写产品名称",
      bimg: "请上传图片",
      simg: "请上传小图",
      costprice:"请填写成本价",
      price: "请填写划线价",
      realprice: "请填写价格",
      stock:  "请填写库存",
      bpid:"请选择大类",
      spid:"请选择小类",
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    hidefrom() {
      this.triggerEvent("change", {
        op: "close",
      })
      // this.triggerEvent("oncloseedit",1)
      this.setData({
        isshow: false
      })
    },
    emtypmodel(restval) {
      var model = {
        simg: [],
        bimg: [],
        name: "",
        about: "",

        phone: "",
        price: "",
        realprice: "",
        status: "1",
        stock: "",
        formattype: "",
        unit: "件",
        contents: {
          cont: "",
          imgs: []
        }
      }
      if (restval) {
        this.setData({
          model
        })
      }
      return model;
    },
    show(op, id) {
      switch (op) {
        case "add": {
          this.setData({
            isshow: true,
            model: this.emtypmodel()
          })
          break;
        }
        case "edit": {
          actclubshop_pro.getmodel(id).then(reslut => {
            if (reslut["status"] == 1) {
              var obj = reslut["obj"];
              obj.costprice = (obj.costprice / 100).toFixed(2)
              obj.realprice = (obj.realprice / 100).toFixed(2)
              obj.price = (obj.price / 100).toFixed(2)

              this.setData({
                isshow: true,
                model: reslut["obj"]
              })
            }
          })
          break;
        }
        case "none": {
          break;
        }
      }
    },
    onClosepopup() {
      this.setData({
        classshow: false
      })
    },
    onconfirmclass(e) {
      var value = e.detail.value;
      var update={classshow: false}
      if(this.data.op=='bclass'){
        update['model.bpid']=value.id
      }else{
        update['model.spid']=value.id
      }
      this.setData(update)
      if(this.data.op=='bclass'){
        this.checkRule("bpid")
      }else{
        this.checkRule("spid")
      }
    },
    chooseclass(e) {
      console.log(e)
      var update = {
        classshow: true
      }
      var op = e.currentTarget.dataset.op;
      update['op'] = op
      var bpid = this.data.model.bpid
      if (op == 'bclass') {
        update['proclass'] = this.data.bclass
      } else {
        console.log("this.data.sclass[bpid]")
        console.log(this.data.sclass)
        console.log(bpid)

        console.log(this.data.sclass[bpid])
        if(this.data.sclass[bpid]){
          update['proclass'] = this.data.sclass[bpid]

        }else{
          update['proclass'] = []

        }

      }
      this.setData(update)
    },
    setval: function (obj, fields, val) {
      var atrrfild = fields.split(".");
      var len = atrrfild.length;
      var tem = obj;
      var i = 0
      for (; i < len - 1; i++) {
        tem = tem[atrrfild[i]]
      }
      tem[atrrfild[i]] = val;
      // checkRule(field)
    },
    changefrom: function (ev) {
      var val = ev.detail
      var field = ev.currentTarget.dataset.field
      console.log(this.data.actmodel)
      console.log(this.data.actmodel)

      this.setval(this.data[this.data.actmodel], field, val)
      this.checkRule(field)


    },
    //验证规则
    checkRule(field) {
      var vildfield = Object.keys(this.data.vildfield);

      if (vildfield.indexOf(field) != -1) {
        var updatemodel = {};
        if((this.data.yihuoconfig.status==1 && (field=='price' || field=='costprice'  || field=='bimg') )){
          updatemodel['vildfield.' + field] = true ;
           
        }else
        if(field=='spid' || field=='bpid' ){
          if(field=='spid' && this.data.level==1){
            updatemodel['vildfield.' + field] = true ;
          }else{
            updatemodel['vildfield.' + field] = this.data.model[field]>0 ? true : false;
          }
        }else
        if (Array.isArray(this.data.model[field])) {
          var len = this.data.model[field].length;
          updatemodel['vildfield.' + field] = len > 0 ? true : false;
        } else {
          updatemodel['vildfield.' + field] = this.data.model[field] ? true : false;
        }
        this.setData(updatemodel)
      }

    },
    //验证表单
    checkForm: function (field) {
      var error = [];
      if (field) {
        this.checkRule(field)
      } else {
        var vildfield = this.data.vildfield;
        Object.keys(vildfield).forEach(item => {
          this.checkRule(item)

          if (!this.data.vildfield[item]) {
           
            error.push(this.data.vildfieldmesage[item])
          }
        })
      }
     
       return error;
      
    },
    saveform: function (ev) {
      var that = this;
      var postmodel = this.data[this.data.actmodel]
      var res = this.checkForm();
      if(this.data.yihuoconfig.status==1){
        postmodel['allowededit']=-1;
        postmodel['status']=-1;

      }

      if (res.length==0) {
        actclubshop_pro.add(postmodel).then(function (result) {
          if (result["status"] == -1) {
            wx.showModal({
              title: result["message"],
              content: result["message"],
              showCancel: false
            })
          } else if (result["status"] == 1) {
            if (postmodel.id) {
              wx.showToast({
                title: '修改成功',
              })
              postmodel['price'] = postmodel['price'] * 100;
              postmodel['realprice'] = postmodel['realprice'] * 100;
              that.triggerEvent("change", {
                op: "update",
                model: postmodel
              })
            } else {
              wx.showModal({
                title: "增加成功",
                content: "增加成功",
                confirmText: "继续增加",
                cancelText: "返回列表",

                success: function (res) {
                  that.triggerEvent("change", {
                    op: "add",
                  })
                  if (res.cancel) {
                    // that.setData({
                    //   isshow: false
                    // })
                    that.hidefrom()


                    //返回列表
                  } else {
                    that.emtypmodel(true)
                    //继续增

                  }

                }
              })
            }



          }



        })
      } else {
        
        wx.showModal({
          title: '错误',
          content: res.join(","),
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
      }

    }


  }
})